﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Table = FCNS.Data.Table;
using DoubleH.Utility;
using System.Data;

namespace StoreS
{
    /// <summary>
    /// PanDianStart.xaml 的交互逻辑
    /// </summary>
    public partial class PanDianStart : Window
    {
        public PanDianStart()
        {
            InitializeComponent();

            InitEvent();
        }

        Table.CheckStoreS order;
        public void Init(Table.CheckStoreS obj)
        {
            order = obj ;
            StartPanDian = false;

            uCGroupS1.IsEditable = false;
            uCUniqueSBrandId.IsEditable = false;
            uCUniqueSPlaceId.IsEditable = false;

            uCGroupS1.Init(Table.GroupS.EnumFlag.商品分类);
            uCUniqueSPlaceId.Init(Table.UniqueS.EnumFlag.产地);
            uCUniqueSBrandId.Init(Table.UniqueS.EnumFlag.品牌);
        }

        public bool StartPanDian { get; set; }

        private void InitEvent()
        {
            buttonOK.Click += (ss, ee) => { PanDian(); };
        }

        private void PanDian()
        {
            if (uCStoreS1.SelectedObjectId == -1)
            {
                MessageWindow.Show("请选择仓库");
                return;
            }
            if (!datePicker1.SelectedDate.HasValue)
            {
                MessageWindow.Show("请选择日期");
                return;
            }

            order.OrderDateTime = datePicker1.SelectedDate.Value;
            order.StoreSId = uCStoreS1.SelectedObjectId;

            StringBuilder sb = new StringBuilder("SELECT   ProductS.Id,ProductSInStoreS.Quantity");
            sb.Append("  FROM ProductS INNER JOIN");
            sb.Append("  ProductSInStoreS ON  ProductS.Id = ProductSInStoreS.ProductSId ");
            sb.Append(" WHERE  (ProductS.Enable = 0)   AND  ProductSInStoreS.StoreSId =" + uCStoreS1.SelectedObjectId);
            if (!string.IsNullOrEmpty(uCGroupS1.Text))
                sb.Append(" and ProductS.GroupSId=" + uCGroupS1.SelectedValue);
            if (!string.IsNullOrEmpty(uCUniqueSPlaceId.Text))
                sb.Append(" and ProductS.PlaceId=" + uCUniqueSPlaceId.SelectedObjectId);
            if (!string.IsNullOrEmpty(uCUniqueSBrandId.Text))
                sb.Append(" and ProductS.BrandId=" + uCUniqueSBrandId.SelectedObjectId);


            foreach (DataRow row in FCNS.Data.SQLdata.GetDataRows(sb.ToString()))
                order.ProductSList.Add(new Table.ProductSInCheckStoreS()
                {
                    ProductSId = (Int64)row[0],
                    QuantityOld = (double)row[1]
                });

        

            MessageWindow.Show(order.Insert().ToString());
            StartPanDian = true;
            this.Close();
        }
    }
}
